<?php
/**
  * wechat php test
  */
require_once './config.php';
require_once './log.php';

require_once './class/WeChat/WeChatService.php';

ini_set("log_errors", 1);

$media_id = $_GET['MediaId'];
$format = $_GET['format'];
$wechat = new WeChatService();
$file = "./data/voices/" . $media_id . ".$format";

$result = false;

if(file_exists($file)) {
	msg("Find media " . $media_id);
	$result = true;
} else {
	msg("Can't find media " . $media_id);
	$result = $wechat->downloadMedia($media_id, $file);
}
if($result) {
	msg("downloading file " . $file);
    header("Connection:keep-alive");
    header("Cache-Control: no-cache, must-revalidate0");
    header("Content-Type: audio/amr");
    header("Content-Length: " .(string)(filesize($file)) );
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    readfile($file);
    exit();
} else {
	msg("File download fail " . $media_id);
	echo "Can't find voice file";
}